package com.cip.admin.action;

import java.util.List;
import java.util.Map;

import com.cip.admin.model.User;
import com.cip.admin.service.FriendsService;
import com.cip.common.util.SecurityUtil;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.interceptor.Interceptor;
import com.opensymphony.xwork2.ActionContext;
import java.util.HashMap;
import javax.servlet.http.HttpServletRequest;

public class MethodInvocationInterceptor implements Interceptor {
	
	private static final long serialVersionUID = 1L;
	
	private FriendsService friendsService;	
	private List<User> userList;	
	List<User> listofSuggests;
	 
    public String intercept(ActionInvocation invocation) throws Exception {
    	ActionContext ac = null;
    	Map<String, Object> parameters = new HashMap<String, Object>();
    	ac = invocation.getInvocationContext();
    	parameters = ac.getParameters();
    	HttpServletRequest request = (HttpServletRequest) ac.get("HTTP_REQUEST");
        String className = invocation.getAction().getClass().getName();
      //  ActionContext ac = invocation.getInvoactionContext();
        long startTime = System.currentTimeMillis();
        System.out.println("Before calling action: " + className);
        User user = SecurityUtil.getLoggedInUser();
        String result = invocation.invoke();
        request.setAttribute("name", "eswar");
        parameters.put("userList", "eswar");
       // parameters.put("listofSuggests", friendsService.getSuggestedFriendsList(user));
        //setUserList(friendsService.getAllFriendsById(user.getId()));
		//setListofSuggests(friendsService.getSuggestedFriendsList(user));
        System.out.println("Afet calling action size: "+parameters.get("userList"));
		//ac.setParameters(parameters);
		
        long endTime = System.currentTimeMillis();
        System.out.println("After calling action: " + className
                + " Time taken: " + (endTime - startTime) + " ms");
 
        return result;
    }
 
    public void destroy() {
        System.out.println("Destroying MyLoggingInterceptor...");
    }
    
    public void init() {
        System.out.println("Initializing MyLoggingInterceptor...");
    }
    
    public FriendsService getFriendsService() {
		return friendsService;
	}

	public void setFriendsService(FriendsService friendsService) {
		this.friendsService = friendsService;
	}
    
    public List<User> getListofSuggests() {
		return listofSuggests;
	}

	public void setListofSuggests(List<User> listofSuggests) {
		this.listofSuggests = listofSuggests;
	}
	
	public List<User> getUserList() {
        return userList;
    }
	
	public void setUserList(List<User> userList) {
		this.userList = userList;
	}	

}
